﻿using Matrix.MaKaYunShang.BLL;
using Matrix.MaKaYunShang.Model;
using Matrix.MaKaYunShang.Model.Dto;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;

namespace Matrix.MaKaYunShang.MagWeb.Controllers
{
    [Matrix.MaKaYunShang.Model.Attributes.Skip]
    public class OrderCommentController : BaseController
    {
        #region 列表页
        public ActionResult List()
        {
            return View();
        }

        [HttpPost]
        [Matrix.MaKaYunShang.Model.Attributes.Skip]
        public JsonResult Listing(int draw, int start, int length)
        {
            try
            {
                int order;
                int.TryParse(Request["order[0][column]"], out order);
                string direction = Request["order[0][dir]"];
                var orderByColumns = new[] {"OrderNo", "NickName", "shopName", "ReplyContent", "PointsStr", "AddTime" };
                string search = Request["search[value]"];

                TableDataViewDto<ViewOrderCommentDto> theList = OrderCommentBLL.Instance.GetPagingList(draw, (start / length) + 1, length, SqlSafe(search),
                    orderByColumns[order] + " " + direction);
                JsonResult jsonResult = Json(theList);
                jsonResult.MaxJsonLength = int.MaxValue;

                return jsonResult;
            }
            catch (Exception ex)
            {
                Logger.Error("Coupon Listing Catch Error: " + ex.Message);
                return Json(false);
            }
        }


        [HttpPost]
        public ActionResult Update(FormCollection collection, OrderComment model)
        {
            if (string.IsNullOrEmpty(model.DealResult))
            {
                return Json(false);
            }
            var orderComment = OrderCommentBLL.Instance.GetModel(model.Id);
            if (orderComment == null) return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            orderComment.DealResult = model.DealResult;
            orderComment.DealTime = DateTime.Now;
            orderComment.IsShopMistake = model.IsShopMistake;
            var result = OrderCommentBLL.Instance.Update(orderComment);
            return Json(result);
        }

        public ActionResult SeeDetails(int id)
        {
            var orderComment = OrderCommentBLL.Instance.GetModel(id);
            return View(orderComment!=null?orderComment:new  OrderComment());
        }
        #endregion
    }
}